/* @ author Anastasia Afonina
 */
// On pressing button 'New Contact' clear fields
function createNewContact() {
    $("#contacts_list").find(':selected').attr('selected', '')

	document.getElementById('Contact_id').textContent = 'New Contact';
	document.getElementById('Profile_Name').value = "";
	document.getElementById('Profile_Surname').value = "";
	document.getElementById('Profile_Date').value = "";
	document.getElementById('Profile_MP').value = "";
	document.getElementById('Profile_HP').value = "";
	document.getElementById('Profile_WP').value = "";
	document.getElementById('Profile_Country').value = "";
}

// Function switches contact with it's data in right span
function switchContact() {
    selected_contact_id = select_element.options[document.getElementById('contacts_list').selectedIndex].value;

    contact_name = javascript_data[selected_contact_id][0];
    contact_surname = javascript_data[selected_contact_id][1];

	document.getElementById('Profile_Name').value = javascript_data[selected_contact_id][0];
	document.getElementById('Profile_Surname').value = javascript_data[selected_contact_id][1];
	document.getElementById('Profile_Date').value = javascript_data[selected_contact_id][3];

    available_phones = javascript_data[selected_contact_id][4];

    work_phone = "";
    home_phone = "";
    mobile_phone = "";

    for (var phone in available_phones)
    {
        if (available_phones[phone][1] == 1)
        {
            home_phone = available_phones[phone][2];
        }
        else if (available_phones[phone][1] == 2)
        {
            work_phone = available_phones[phone][2];
        }
        else if (available_phones[phone][1] == 3)
        {
            mobile_phone = available_phones[phone][2];
        }
    }
	document.getElementById('Profile_MP').value = mobile_phone;
	document.getElementById('Profile_HP').value = home_phone;
	document.getElementById('Profile_WP').value = work_phone;

    country = javascript_data[selected_contact_id][5][0][1];
    if(typeof country === 'undefined'){
        country = ""
    }
    city = javascript_data[selected_contact_id][5][0][2];
    if(typeof city === 'undefined'){
        city = ""
    }
    apartment = javascript_data[selected_contact_id][5][0][4];
    if(typeof apartment === 'undefined'){
        apartment = ""
    }
	document.getElementById('Profile_Country').value = country;
    document.getElementById('Profile_City').value = city;
    document.getElementById('Profile_Apartment').value = apartment;

	$('legend#Contact_id').text(contact_name + " " + contact_surname + "'s Profile");
};

// Assign <legend> of form when switching contacts from the list using
// switchContact function
$(document).ready(function() {
	$('#contacts_list').change(switchContact);
});

// Show fields for additional Home number after selecting check-box
function addHomeNumber() {
	if (document.getElementById('Profile_inlineCheckbox1').checked == true) {
		$("#Profile_HPtext, #Profile_HP").show();
	} else {
		$("#Profile_HPtext, #Profile_HP").hide();
	}
};

// Show fields for additional Work number after selecting check-box
function addWorkNumber() {
	if (document.getElementById('Profile_inlineCheckbox2').checked == true) {
		$("#Profile_WPtext, #Profile_WP").show();
	} else {
		$("#Profile_WPtext, #Profile_WP").hide();
	}
};

function SaveProfile() {
    document.getElementById('profile_form').submit()
}